package view.editorview;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;
import model.editormodel.EditorModel;
import view.UtilityConstants;
import view.commonview.InfoPanel;
import view.commonview.TabbedPane;
import controller.commoncontroller.CircuitInfoListener;
import controller.commoncontroller.ComponentExportListener;
import controller.commoncontroller.LoadCircuitListener;
import controller.commoncontroller.NewCircuitListener;
import controller.commoncontroller.PictureExportListener;
import controller.commoncontroller.RasterElementsListener;
import controller.commoncontroller.SaveCircuitListener;
import controller.commoncontroller.SettingsListener;
import controller.editorcontroller.AutoCreateSwitchListener;
import controller.editorcontroller.AutoCreateLampListener;

/**
 * <b>Erbt:</b> <code>JPanel</code> <br>
 * <b>Funktion:</b> Repraesentation Werkzeugleiste<br>
 * <br>
 * Panel, auf dem haeufig genutzte Funktionen des Menues als Buttons liegen.
 * 
 * @author Team Locsi
 * 
 */
@SuppressWarnings("serial")
public class EditorToolPanel extends JToolBar {

	private JButton newC = new JButton(new ImageIcon(getClass().getClassLoader().getResource(UtilityConstants.ICON_PATH
			+ "new.png")));
	private JButton loadC = new JButton(new ImageIcon(getClass().getClassLoader().getResource(
			UtilityConstants.ICON_PATH + "load.png")));
	private JButton saveC = new JButton(new ImageIcon(getClass().getClassLoader().getResource(
			UtilityConstants.ICON_PATH + "saveNew.png")));
	private JButton comp = new JButton(new ImageIcon(getClass().getClassLoader().getResource(UtilityConstants.ICON_PATH
			+ "deviceNew.png")));
	private JButton pic = new JButton(new ImageIcon(getClass().getClassLoader().getResource(UtilityConstants.ICON_PATH
			+ "cam.png")));
	private JButton check = new JButton(new ImageIcon(getClass().getClassLoader().getResource(
			UtilityConstants.ICON_PATH + "checkNew.png")));
	private JButton info = new JButton(new ImageIcon(getClass().getClassLoader().getResource(UtilityConstants.ICON_PATH
			+ "info32.png")));
	// private JButton raster = new JButton(new ImageIcon(
	// UtilityConstants.ICON_PATH + "grid.png"));
	private JButton magicInsertSwitch = new JButton(new ImageIcon(getClass().getClassLoader().getResource(
			UtilityConstants.ICON_PATH + "magic-insert-sw.png")));
	private JButton magicInsertLamp = new JButton(new ImageIcon(getClass().getClassLoader().getResource(
			UtilityConstants.ICON_PATH + "magic-insert-la.png")));
	private JButton setraster = new JButton(new ImageIcon(getClass().getClassLoader().getResource(
			UtilityConstants.ICON_PATH + "rasterB.png")));
	private JButton settings = new JButton(new ImageIcon(getClass().getClassLoader().getResource(
			UtilityConstants.ICON_PATH + "settingsNew.png")));
	private Dimension filler = new Dimension(30, 0);

	/**
	 * Dieser Konstruktor erzeugt ein neues <code>EditorToolPanel</code>-Objekt.
	 * 
	 * @param tabs
	 *            <code>TabbedPane</code> - Referenz auf das
	 *            <code>TabbedPane</code>
	 * @param eModel
	 *            <code>EditorModel</code> - Referenz auf das
	 *            <code>EditorModel</code>
	 * @param editorArea
	 *            <code>EditorArea</code> - Referenz auf die
	 *            <code>EditorArea</code>
	 */
	public EditorToolPanel(TabbedPane tabs, final EditorModel eModel,
			final EditorArea editorArea, InfoPanel infoPanel) {
		super(JToolBar.HORIZONTAL);

		this.setBackground(new Color(238, 238, 238));
		this.add(Box.createHorizontalGlue());
		// neue Schaltung erzeugen
		newC.addActionListener(new NewCircuitListener(eModel));
		this.add(newC);
		newC.setToolTipText("Neue Schaltung");
		newC.setOpaque(false);
		newC.setContentAreaFilled(false);
		newC.setBorder(BorderFactory.createEmptyBorder());
		this.add(Box.createRigidArea(filler));

		// Schaltung laden
		loadC.addActionListener(new LoadCircuitListener(eModel));
		this.add(loadC);
		loadC.setToolTipText("Schaltung laden");
		loadC.setOpaque(false);
		loadC.setContentAreaFilled(false);
		loadC.setBorder(BorderFactory.createEmptyBorder());
		this.add(Box.createRigidArea(filler));

		// Schaltung speichern
		saveC.addActionListener(new SaveCircuitListener(eModel, tabs
				.getIPanel().getInfoArea()));
		this.add(saveC);
		saveC.setToolTipText("Speichern");
		saveC.setOpaque(false);
		saveC.setContentAreaFilled(false);
		saveC.setBorder(BorderFactory.createEmptyBorder());
		this.add(Box.createRigidArea(filler));

		// Bauteil exportieren
		comp.addActionListener(new ComponentExportListener(eModel));
		this.add(comp);
		comp.setToolTipText("Schaltung als Bauteil exportieren");
		comp.setOpaque(false);
		comp.setContentAreaFilled(false);
		comp.setBorder(BorderFactory.createEmptyBorder());
		this.add(Box.createRigidArea(filler));
		pic.addActionListener(new PictureExportListener(tabs));

		// Schaltung als Bild exportieren
		this.add(pic);
		pic.setToolTipText("Schaltung als Bild exportieren");
		pic.setOpaque(false);
		pic.setContentAreaFilled(false);
		pic.setMargin(new Insets(150, 150, 150, 150));
		pic.setBorder(BorderFactory.createEmptyBorder());
		this.add(Box.createRigidArea(filler));
		this.add(new JToolBar.Separator());

		// Testen der Schaltung auf Fehler
		this.add(check);
		check.setToolTipText("Schaltung auf Fehler überprüfen");
		check.setOpaque(false);
		check.setContentAreaFilled(false);
		check.setBorder(BorderFactory.createEmptyBorder());
		this.add(Box.createRigidArea(filler));
		info.addActionListener(new CircuitInfoListener(infoPanel.getInfoArea()));

		// Schaltungsinformation
		this.add(info);
		info.setToolTipText("Schaltungsinformation anzeigen");
		info.setOpaque(false);
		info.setContentAreaFilled(false);
		info.setBorder(BorderFactory.createEmptyBorder());
		this.add(Box.createRigidArea(filler));
		this.add(new JToolBar.Separator());

		// Raster ein-/ausblenden
		// this.add(raster);
		// raster.setToolTipText("Raster ein-/ausblenden");
		// raster.setOpaque(false);
		// raster.setContentAreaFilled(false);
		// raster.setBorder(BorderFactory.createEmptyBorder());
		// raster.addActionListener(new SetRasterVisibleListener(editorArea));
		// this.add(Box.createRigidArea(filler));

		// Schalter an unverbundenen Eingängen erzeugen
		this.add(magicInsertSwitch);
		magicInsertSwitch.setToolTipText("Schalter erzeugen");
		magicInsertSwitch.setOpaque(false);
		magicInsertSwitch.setContentAreaFilled(false);
		magicInsertSwitch.setBorder(BorderFactory.createEmptyBorder());
		magicInsertSwitch.addActionListener(new AutoCreateSwitchListener(eModel,
				editorArea, infoPanel.getInfoArea()));
		this.add(Box.createRigidArea(filler));
		
		// Lampen an komplett unverbundenen Ausgaengen erzeugen
		this.add(magicInsertLamp);
		magicInsertLamp.setToolTipText("Lampen erzeugen");
		magicInsertLamp.setOpaque(false);
		magicInsertLamp.setContentAreaFilled(false);
		magicInsertLamp.setBorder(BorderFactory.createEmptyBorder());
		magicInsertLamp.addActionListener(new AutoCreateLampListener(eModel,
				editorArea, infoPanel.getInfoArea()));
		this.add(Box.createRigidArea(filler));
		

		// Schaltung am Raster ausrichten
		this.add(setraster);
		setraster.setToolTipText("Am Raster ausrichten");
		setraster.setOpaque(false);
		setraster.setContentAreaFilled(false);
		setraster.setBorder(BorderFactory.createEmptyBorder());
		setraster.addActionListener(new RasterElementsListener(editorArea,
				eModel));
		this.add(Box.createRigidArea(filler));
		this.add(new JToolBar.Separator());

		// Einstellungen
		this.add(settings);
		settings.setToolTipText("Einstellungen");
		settings.setOpaque(false);
		settings.setContentAreaFilled(false);
		settings.setBorder(BorderFactory.createEmptyBorder());
		settings.addActionListener(new SettingsListener(tabs));
		this.add(Box.createHorizontalGlue());
	}

	/**
	 * Gibt den Check-Button zurueck
	 * 
	 * @return Check-Button
	 */
	public JButton getCheckButton() {
		return check;
	}
	
	@Override
	public void setEnabled(boolean enabled) {
	    super.setEnabled(enabled);
	    Component[] components = getComponents();
	    if (components != null && components.length > 0) {
	    int count = components.length;
	    for (int i = 0; i < count; i++)
	        components[i].setEnabled(enabled);
	    }
	}

}